﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace FirstFloor.P2P.TestApp
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();

            P2PApplication.Current.ConnectionTypeChanged += OnConnectionTypeChanged;
            P2PApplication.Current.DataReceived += OnDataReceived;
            P2PApplication.Current.IMReceived += OnIMReceived;
            P2PApplication.Current.SendDataFailed += OnSendDataFailed;
            P2PApplication.Current.Shutdown += OnShutdown;
            P2PApplication.Current.StateChanged += OnStateChanged;

            if (P2PApplication.Current.P2PNetworkAvailable) {
                P2PApplication.Current.Connect();
            }
            else {
                Trace("P2P network not available");
            }
        }

        [ScriptableMember]
        public void DebugMessage(string message)
        {
            Trace(message);
        }

        private void Trace(string message, params object[] o)
        {
            message = string.Format(message, o);

            this.log.Text += string.Format("{0:hh:mm:ss.fff}  {1}\r\n", DateTime.Now, message);
        }

        private void OnConnectionTypeChanged(object sender, P2PConnectionTypeEventArgs e)
        {
            Trace("OnConnectionTypeChanged({0})", e.Type);
        }

        private void OnDataReceived(object sender, P2PValueEventArgs e)
        {
            Trace("OnDataReceived({0})", e.Value);
        }

        private void OnIMReceived(object sender, P2PValueEventArgs e)
        {
            Trace("OnIMReceived({0})", e.Value);
        }

        private void OnSendDataFailed(object sender, P2PSendDataFailedEventArgs e)
        {
            Trace("OnSendDataFailed({0}, {1})", e.Type, e.Value);
        }

        private void OnShutdown(object sender, EventArgs e)
        {
            Trace("OnShutdown");
        }

        private void OnStateChanged(object sender, P2PApplicationStateEventArgs e)
        {
            Trace("OnStateChanged({0}, {1})", e.State, e.CloseReason);

            if (e.State == P2PApplicationState.Connected) {
                foreach (P2PUser user in P2PApplication.Current.Users) {
                    Trace("User: {0}\r\nEmail: {1}\r\nLocalIP: {2}\r\nGlobalIP: {3}\r\nPUID: {4}\r\nIsMe: {5}\r\nIsInviter: {6}",
                        user.Name, user.Email, user.LocalIP, user.GlobalIP, user.PUID, user.IsMe, user.IsInviter);
                }
            }

            btnEnterIM.IsEnabled = e.State == P2PApplicationState.Connected;
            btnSendIM.IsEnabled = e.State == P2PApplicationState.Connected;
            btnSendIMAsUser.IsEnabled = e.State == P2PApplicationState.Connected;
            btnSendData.IsEnabled = e.State == P2PApplicationState.Connected;
        }

        private void btnEnterIM_Click(object sender, RoutedEventArgs e)
        {
            try {
                P2PApplication.Current.EnterIM(txtInput.Text);
            }
            catch (Exception ex) {
                Trace(ex.ToString());
            }
        }

        private void btnSendIM_Click(object sender, RoutedEventArgs e)
        {
            try {
                P2PApplication.Current.SendIM(txtInput.Text);
            }
            catch (Exception ex) {
                Trace(ex.ToString());
            }
        }

        private void btnSendIMAsUser_Click(object sender, RoutedEventArgs e)
        {
            try {
                P2PApplication.Current.SendIMAsUser(txtInput.Text);
            }
            catch (Exception ex) {
                Trace(ex.ToString());
            }
        }

        private void btnSendData_Click(object sender, RoutedEventArgs e)
        {
            try {
                P2PApplication.Current.SendData(txtInput.Text);
            }
            catch (Exception ex) {
                Trace(ex.ToString());
            }
        }
    }
}
